package com.yxhl.zoume.common.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yxhl.zoume.R;
import com.yxhl.zoume.ZouMeApplication;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.main.info.MainEntrance;
import com.yxhl.zoume.core.main.model.MainEntranceInfo;

import butterknife.BindView;

public class AdvertisementActivity extends BaseActivityWithToolBar {

    private static final String TAGET_URL = "TARGET_URL";
    @BindView(R.id.wv_ad_show)
    WebView mWvAdShow;
    private MainEntranceInfo mMainEntranceInfo;
    private String mUrl;

    public static Intent getCallingIntent(Activity activity, String targetUrl) {
        Intent intent = new Intent(activity, AdvertisementActivity.class);
        intent.putExtra(TAGET_URL, targetUrl);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        handleIntent();
        mWvAdShow.getSettings().setAppCacheEnabled(true);
        mWvAdShow.getSettings().setSupportZoom(true);
        mWvAdShow.getSettings().setJavaScriptEnabled(true);
        mWvAdShow.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        mWvAdShow.loadUrl(mUrl);
        mWvAdShow.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        setToolbarTitle("活动");

    }


    private void handleIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mUrl = (String) intent.getSerializableExtra(TAGET_URL);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ad;
    }

    @Override
    protected void initializeInjector() {

    }

    @Override
    protected void onHeaderLeftClick(View view) {
    onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        enterApp();
    }

    private void enterApp() {

        if (mMainEntranceInfo == null) {
            mMainEntranceInfo = new MainEntranceInfo<>(MainEntrance.SPLASH);
        }
        ZouMeApplication.application.getAppComponent().navigator().navigateToMain(this, mMainEntranceInfo);
        finish();
        overridePendingTransition(R.anim.noanimation, R.anim.noanimation);
    }

}
